/**
 * coding = utf-8
 *
 * @author :今夕
 * @Time :2025/10/21 08:45
 * @file :Demo.java
 * @software :IntelliJ IDEA
 */
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        try {
            // 1. 新增数据
            String insertSql = "INSERT INTO student (id,name, age, gender) VALUES (?, ?, ?,?)";
            int rows = DBUtils.executeUpdate(insertSql, "12253", "测试", 18, "男");
            System.out.println("新增了" + rows + "条数据");

            // 2. 查询数据
            String querySql = "SELECT id,name, age, gender,hobby,course,date FROM student order by date";
            List<Student> students = DBUtils.executeQuery(querySql, rs -> {List<Student> list = new ArrayList<>();
                while (rs.next()) {
                    Student s = new Student();
                    s.setId(rs.getString("id"));
                    s.setName(rs.getString("name"));
                    s.setAge(rs.getInt("age"));
                    s.setGender(rs.getString("gender"));
                    list.add(s);
                }
                return list;
            }); // 查询年龄小于25的学生
            // 打印查询结果
            for (Student s : students) {
                System.out.println(s.getId() + "：" + s.getName() + "，" + s.getAge() + "岁");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 学生实体类
    static class Student {
        private String id;
        private String name;
        private int age;
        public String gender;

        // getter和setter
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public int getAge() { return age; }
        public void setAge(int age) { this.age = age; }


        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }
    }
}